package pers.lonestar.framework.utils.support.write;

import java.io.File;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import pers.lonestar.framework.utils.support.enums.CellDataTypeEnum;
import pers.lonestar.framework.utils.support.metadata.write.WriteWorkbook;
import pers.lonestar.framework.utils.support.utils.CellStyleUtil;

public class ExcelWriterBuilder {
    private final WriteWorkbook writeWorkbook;

    public ExcelWriterBuilder() {
        this.writeWorkbook = new WriteWorkbook();
    }

    public ExcelWriterSheetBuilder sheet() {
        return new ExcelWriterSheetBuilder(build());
    }

    public ExcelWriterSheetBuilder sheet(String sheetName) {
        ExcelWriterSheetBuilder writerSheetBuilder = new ExcelWriterSheetBuilder(build());
        return writerSheetBuilder.sheetName(sheetName);
    }

    public ExcelWriterSheetBuilder sheet(String sheetName, Class<?> headClass) {
        ExcelWriterSheetBuilder writerSheetBuilder = new ExcelWriterSheetBuilder(build());
        return writerSheetBuilder
                .sheetName(sheetName)
                .head(headClass);
    }

    public ExcelWriterSheetBuilder sheet(String sheetName, List<String> headList) {
        ExcelWriterSheetBuilder writerSheetBuilder = new ExcelWriterSheetBuilder(build());
        return writerSheetBuilder
                .sheetName(sheetName)
                .head(headList, headList);
    }

    public ExcelWriterSheetBuilder sheet(String sheetName, List<String> headFieldList, List<String> headNameList) {
        ExcelWriterSheetBuilder writerSheetBuilder = new ExcelWriterSheetBuilder(build());
        return writerSheetBuilder
                .sheetName(sheetName)
                .head(headFieldList, headNameList);
    }

    public ExcelWriterBuilder file(File file) {
        writeWorkbook.setFile(file);
        return this;
    }

    public ExcelWriterBuilder file(String fileName) {
        writeWorkbook.setFile(new File(fileName));
        return this;
    }

    public ExcelWriterBuilder file(OutputStream outputStream) {
        writeWorkbook.setOutputStream(outputStream);
        return this;
    }

    public ExcelWriter build() {
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        CellStyle headStyle = CellStyleUtil.getHeadStyle(workbook);
        Map<CellDataTypeEnum, CellStyle> contentStyleMap = CellStyleUtil.getContentStyleMap(workbook);
        writeWorkbook.setHeadCellStyle(headStyle);
        writeWorkbook.setContentCellStyleMap(contentStyleMap);
        writeWorkbook.setWorkbook(workbook);
        return new ExcelWriter(writeWorkbook);
    }
}
